{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#bgr8转jpeg格式\n",
    "import enum\n",
    "import cv2\n",
    "import time\n",
    "# import the necessary packages\n",
    "#import simple_barcode_detection\n",
    "import cv2\n",
    "import numpy as np\n",
    "import pyzbar.pyzbar as pyzbar\n",
    "from PIL import Image\n",
    "import ipywidgets.widgets as widgets\n",
    "\n",
    "#-*- coding:UTF-8 -*-\n",
    "import RPi.GPIO as GPIO\n",
    "import time\n",
    "\n",
    "def bgr8_to_jpeg(value, quality=75):\n",
    "    return bytes(cv2.imencode('.jpg', value)[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3/dist-packages/ipykernel_launcher.py:45: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.\n"
     ]
    }
   ],
   "source": [
    "#避障相关\n",
    "# 小于30的避障原地速度可以更加激进\n",
    "import RPi.GPIO as GPIO\n",
    "import time\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "white_list = ['stop']\n",
    "\n",
    "#是否发现有效二维码\n",
    "find_qrcode = False\n",
    "#是否鸣笛警告\n",
    "waring = False\n",
    "\n",
    "\n",
    "\n",
    "# 小车电机引脚定义\n",
    "IN1 = 20\n",
    "IN2 = 21\n",
    "IN3 = 19\n",
    "IN4 = 26\n",
    "ENA = 16\n",
    "ENB = 13\n",
    "\n",
    "# 超声波引脚定义\n",
    "EchoPin = 0\n",
    "TrigPin = 1\n",
    "\n",
    "# RGB三色灯引脚定义\n",
    "LED_R = 22\n",
    "LED_G = 27\n",
    "LED_B = 24\n",
    "\n",
    "# 舵机引脚定义\n",
    "ServoPin = 23\n",
    "\n",
    "# 红外避障引脚定义\n",
    "AvoidSensorLeft = 12\n",
    "AvoidSensorRight = 17\n",
    "\n",
    "buzzer = 8\n",
    "# 设置GPIO口为BCM编码方式\n",
    "GPIO.setmode(GPIO.BCM)\n",
    "GPIO.setup(buzzer,GPIO.OUT) \n",
    "# 忽略警告信息\n",
    "GPIO.setwarnings(False)\n",
    "\n",
    "\n",
    "\n",
    "# 电机引脚初始化为输出模式\n",
    "# 按键引脚初始化为输入模式\n",
    "# 超声波,RGB三色灯,舵机引脚初始化\n",
    "# 红外避障引脚初始化\n",
    "def init():\n",
    "    global pwm_ENA\n",
    "    global pwm_ENB\n",
    "    global pwm_servo\n",
    "    GPIO.setup(ENA, GPIO.OUT, initial=GPIO.HIGH)\n",
    "    GPIO.setup(IN1, GPIO.OUT, initial=GPIO.LOW)\n",
    "    GPIO.setup(IN2, GPIO.OUT, initial=GPIO.LOW)\n",
    "    GPIO.setup(ENB, GPIO.OUT, initial=GPIO.HIGH)\n",
    "    GPIO.setup(IN3, GPIO.OUT, initial=GPIO.LOW)\n",
    "    GPIO.setup(IN4, GPIO.OUT, initial=GPIO.LOW)\n",
    "    GPIO.setup(EchoPin, GPIO.IN)\n",
    "    GPIO.setup(TrigPin, GPIO.OUT)\n",
    "    GPIO.setup(LED_R, GPIO.OUT)\n",
    "    GPIO.setup(LED_G, GPIO.OUT)\n",
    "    GPIO.setup(LED_B, GPIO.OUT)\n",
    "    GPIO.setup(ServoPin, GPIO.OUT)\n",
    "    GPIO.setup(AvoidSensorLeft, GPIO.IN)\n",
    "    GPIO.setup(AvoidSensorRight, GPIO.IN)\n",
    "    \n",
    "    # 设置pwm引脚和频率为2000hz\n",
    "    pwm_ENA = GPIO.PWM(ENA, 2000)\n",
    "    pwm_ENB = GPIO.PWM(ENB, 2000)\n",
    "    pwm_ENA.start(0)\n",
    "    pwm_ENB.start(0)\n",
    "    # 设置舵机的频率和起始占空比\n",
    "    pwm_servo = GPIO.PWM(ServoPin, 50)\n",
    "    pwm_servo.start(0)\n",
    "\n",
    "\n",
    "# 小车前进\n",
    "def run(leftspeed, rightspeed):\n",
    "    GPIO.output(IN1, GPIO.HIGH)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.HIGH)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(leftspeed)\n",
    "    pwm_ENB.ChangeDutyCycle(rightspeed)\n",
    "\n",
    "\n",
    "# 小车后退\n",
    "def back(leftspeed, rightspeed):\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.HIGH)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.HIGH)\n",
    "    pwm_ENA.ChangeDutyCycle(leftspeed)\n",
    "    pwm_ENB.ChangeDutyCycle(rightspeed)\n",
    "\n",
    "\n",
    "# 小车左转\n",
    "def left(leftspeed, rightspeed):\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.HIGH)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(leftspeed)\n",
    "    pwm_ENB.ChangeDutyCycle(rightspeed)\n",
    "\n",
    "\n",
    "# 小车右转\n",
    "def right(leftspeed, rightspeed):\n",
    "    GPIO.output(IN1, GPIO.HIGH)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(leftspeed)\n",
    "    pwm_ENB.ChangeDutyCycle(rightspeed)\n",
    "\n",
    "\n",
    "# 小车原地左转\n",
    "def spin_left(leftspeed, rightspeed):\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.HIGH)\n",
    "    GPIO.output(IN3, GPIO.HIGH)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(leftspeed)\n",
    "    pwm_ENB.ChangeDutyCycle(rightspeed)\n",
    "\n",
    "\n",
    "# 小车原地右转\n",
    "def spin_right(leftspeed, rightspeed):\n",
    "    GPIO.output(IN1, GPIO.HIGH)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.HIGH)\n",
    "    pwm_ENA.ChangeDutyCycle(leftspeed)\n",
    "    pwm_ENB.ChangeDutyCycle(rightspeed)\n",
    "\n",
    "\n",
    "# 小车停止\n",
    "def brake():\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "\n",
    "def brake_time(delaytime):\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(80)\n",
    "    pwm_ENB.ChangeDutyCycle(80)\n",
    "    time.sleep(delaytime)\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "    \n",
    "def run_time(delaytime):\n",
    "    GPIO.output(IN1, GPIO.HIGH)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.HIGH)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(5)\n",
    "    pwm_ENB.ChangeDutyCycle(5)\n",
    "    time.sleep(delaytime)\n",
    "\n",
    "#小车后退\n",
    "def back_time(delaytime):\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.HIGH)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.HIGH)\n",
    "    pwm_ENA.ChangeDutyCycle(5)\n",
    "    pwm_ENB.ChangeDutyCycle(5)\n",
    "    time.sleep(delaytime)\n",
    "\n",
    "#小车左转\t\n",
    "def left_time(delaytime):\n",
    "    GPIO.output(IN1, GPIO.LOW)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.HIGH)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(5)\n",
    "    pwm_ENB.ChangeDutyCycle(5)\n",
    "    time.sleep(delaytime)\n",
    "\n",
    "#小车右转\n",
    "def right_time(delaytime):\n",
    "    GPIO.output(IN1, GPIO.HIGH)\n",
    "    GPIO.output(IN2, GPIO.LOW)\n",
    "    GPIO.output(IN3, GPIO.LOW)\n",
    "    GPIO.output(IN4, GPIO.LOW)\n",
    "    pwm_ENA.ChangeDutyCycle(5)\n",
    "    pwm_ENB.ChangeDutyCycle(5)\n",
    "    time.sleep(delaytime)\n",
    "    \n",
    "    \n",
    "#小车鸣笛\n",
    "def whistle():   \n",
    "    GPIO.output(buzzer, GPIO.LOW)\n",
    "    time.sleep(0.1)\n",
    "    GPIO.output(buzzer, GPIO.HIGH)\n",
    "    time.sleep(0.001)  \n",
    "\n",
    "# #鸣笛15s\n",
    "# def whistle_15s():\n",
    "#     #每个线程需要单独设置端口功能\n",
    "#     GPIO.setup(buzzer,GPIO.OUT,initial=GPIO.HIGH)\n",
    "#     start_time = time.time()\n",
    "#     while time.time() - start_time < 15 and warning:\n",
    "#         whistle()\n",
    "\n",
    "\n",
    "\n",
    "def color_led(red, green, blue):\n",
    "    GPIO.output(LED_R, red)\n",
    "    GPIO.output(LED_G, green)\n",
    "    GPIO.output(LED_B, blue)\n",
    "    time.sleep(0.5)\n",
    "\n",
    "\n",
    "def wink():\n",
    "    GPIO.setup(LED_R, GPIO.OUT)\n",
    "    GPIO.setup(LED_G, GPIO.OUT)\n",
    "    GPIO.setup(LED_B, GPIO.OUT)\n",
    "#     while True:\n",
    "    for i in range(2):\n",
    "        color_led(0, 0, 255)\n",
    "        color_led(255, 0, 0)\n",
    "        color_led(255, 255, 0)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 超声波函数\n",
    "def Distance_test():\n",
    "    GPIO.output(TrigPin, GPIO.HIGH)\n",
    "    time.sleep(0.000015)\n",
    "    GPIO.output(TrigPin, GPIO.LOW)\n",
    "    while not GPIO.input(EchoPin):\n",
    "        pass\n",
    "    t1 = time.time()\n",
    "    while GPIO.input(EchoPin):\n",
    "        pass\n",
    "    t2 = time.time()\n",
    "    print(\"distance is %d \" % (((t2 - t1) * 340 / 2) * 100))\n",
    "    time.sleep(0.01)\n",
    "    return ((t2 - t1) * 340 / 2) * 100\n",
    "\n",
    "\n",
    "# 舵机旋转到指定角度\n",
    "def servo_appointed_detection(pos):\n",
    "    for i in range(18):\n",
    "        pwm_servo.ChangeDutyCycle(2.5 + 10 * pos / 180)\n",
    "\n",
    "\n",
    "# 舵机旋转超声波测距避障，led根据车的状态显示相应的颜色\n",
    "def servo_color_carstate():\n",
    "    # 开红灯\n",
    "    GPIO.output(LED_R, GPIO.HIGH)\n",
    "    GPIO.output(LED_G, GPIO.LOW)\n",
    "    GPIO.output(LED_B, GPIO.LOW)\n",
    "    back(5, 5)\n",
    "    time.sleep(0.08)\n",
    "    brake()\n",
    "\n",
    "    # 舵机旋转到0度，即右侧，测距\n",
    "    servo_appointed_detection(0)\n",
    "    time.sleep(0.8)\n",
    "    rightdistance = Distance_test()\n",
    "\n",
    "    # 舵机旋转到180度，即左侧，测距\n",
    "    servo_appointed_detection(180)\n",
    "    time.sleep(0.8)\n",
    "    leftdistance = Distance_test()\n",
    "\n",
    "    # 舵机旋转到90度，即前方，测距\n",
    "    servo_appointed_detection(90)\n",
    "    time.sleep(0.8)\n",
    "    frontdistance = Distance_test()\n",
    "\n",
    "    if leftdistance < 60 and rightdistance < 60 and frontdistance < 60:\n",
    "        # 亮品红色，掉头\n",
    "        GPIO.output(LED_R, GPIO.HIGH)\n",
    "        GPIO.output(LED_G, GPIO.LOW)\n",
    "        GPIO.output(LED_B, GPIO.HIGH)\n",
    "        spin_right(3,3)\n",
    "#         right(6,6)\n",
    "#         right_time(1)\n",
    "#         left(5, 5)\n",
    "#         run_time(1)\n",
    "#         right(5,5)\n",
    "#         run_time(1)\n",
    "#         right(5,5)\n",
    "#         print(\"两边小于60000000000000000000000000000000\")\n",
    "        time.sleep(0.58)\n",
    "    elif leftdistance >= rightdistance:\n",
    "        # 亮蓝色\n",
    "        GPIO.output(LED_R, GPIO.LOW)\n",
    "        GPIO.output(LED_G, GPIO.LOW)\n",
    "        GPIO.output(LED_B, GPIO.HIGH)\n",
    "        print(\"左LLLLLLLLLLLLLLLLL\")\n",
    "        spin_left(3, 3)\n",
    "#         left(5, 5)\n",
    "#         run_time(1)\n",
    "#         right(5,5)\n",
    "#         run_time(1)\n",
    "#         right(5,5)\n",
    "    \n",
    "#         left_time(2)\n",
    "        time.sleep(0.28)\n",
    "    elif leftdistance <= rightdistance:\n",
    "        # 亮品红色，向右转\n",
    "        GPIO.output(LED_R, GPIO.HIGH)\n",
    "        GPIO.output(LED_G, GPIO.LOW)\n",
    "        GPIO.output(LED_B, GPIO.HIGH)\n",
    "        print(\"右RRRRRRRRRRRRRR\")\n",
    "        spin_right(3, 3)\n",
    "#         right(5, 5)\n",
    "#         run_time(1)\n",
    "#         left(5,5)\n",
    "#         run_time(1)\n",
    "#         left(5,5)\n",
    "# #         right_time(1)\n",
    "        time.sleep(0.28)\n",
    "\n",
    "\n",
    "# 延时2s\n",
    "time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_control(info):\n",
    "#     or info == \"[2]\" or info == \"[3]\" or info == \"[4]\"\n",
    "    if info == \"[0]\" or info == \"[1]\":\n",
    "        brake_time(3)\n",
    "    else:\n",
    "        run(5,5)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[INFO] loading network...\n"
     ]
    }
   ],
   "source": [
    "from keras.preprocessing.image import img_to_array\n",
    "from keras.models import load_model\n",
    "import numpy as np\n",
    "import argparse\n",
    "import imutils\n",
    "import cv2\n",
    "\n",
    "norm_size = 64\n",
    "\n",
    "# print(\"[INFO] loading network...\")\n",
    "# model = load_model(args[\"model\"])\n",
    "# 预测函数，\n",
    "# 输入： 包含配置参数的字典\n",
    "print(\"[INFO] loading network...\")\n",
    "model = load_model('./MODE/traffic_sign.model')\n",
    "def predict(args):\n",
    "    # 加载训练好的卷积神经网络\n",
    "#     print(\"[INFO] loading network...\")\n",
    "#     model = load_model(args[\"model\"])\n",
    "\n",
    "    # 加载图像\n",
    "    #image = cv2.imread(args[\"image\"])\n",
    "    image = args[\"image\"]\n",
    "    # 因为对图像需要进行写入标签，影响较大所以复制一个图像\n",
    "    orig = image.copy()\n",
    "\n",
    "    # 预处理图像进行分类\n",
    "    # 图像的尺寸重载\n",
    "    image = cv2.resize(image, (norm_size, norm_size))\n",
    "    # 图像的序列的归一化处理\n",
    "    image = image.astype(\"float\") / 255.0\n",
    "    # 将图像进行序列化\n",
    "    image = img_to_array(image)\n",
    "    # 展开数组的形状.\n",
    "    # 插入一个新的轴，该轴将出现在扩展阵列形状的轴位置\n",
    "    image = np.expand_dims(image, axis=0)\n",
    "\n",
    "    # 对输入的图像进行分类\n",
    "    result = model.predict(image)[0]\n",
    "    # print (result.shape)\n",
    "    proba = np.max(result)\n",
    "    label = str(np.where(result == proba)[0])\n",
    "    sig=label\n",
    "    print(sig)\n",
    "    label = \"{}: {:.2f}%\".format(label, proba * 100)\n",
    "    print(label)\n",
    "    detect_control(sig)\n",
    "    return sig\n",
    "#     # 在需要加载图像的情况下\n",
    "#     if args['show']:\n",
    "#         output = imutils.resize(orig, width=400)\n",
    "#         # 在图像上绘制标签字符串\n",
    "#         cv2.putText(output, label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX,\n",
    "#                     0.7, (0, 255, 0), 2)\n",
    "#         # 显示带标签的图像\n",
    "#         cv2.imshow(\"Output\", output)\n",
    "#         cv2.waitKey(0)\n",
    "\n",
    "def detect():\n",
    "#     init()\n",
    "    print(\"检测开始\")\n",
    "    camera = cv2.VideoCapture(0)\n",
    "    camera.set(3, 320)\n",
    "    camera.set(4, 240)\n",
    "    camera.set(5, 120)  #设置帧率\n",
    "    print(\"middle  _______________________________\")\n",
    "    # fourcc = cv2.VideoWriter_fourcc(*\"MPEG\")\n",
    "    camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))\n",
    "    camera.set(cv2.CAP_PROP_BRIGHTNESS, 40) #设置亮度 -64 - 64  0.0\n",
    "    camera.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64  2.0\n",
    "    camera.set(cv2.CAP_PROP_EXPOSURE, 156) #设置曝光值 1.0 - 5000  156.0\n",
    "    ret, frame = camera.read()\n",
    "    #image_widget.value = bgr8_to_jpeg(frame)\n",
    "    args = {}\n",
    "    # 读取当前帧\n",
    "    ret, frame = camera.read()\n",
    "    print(\"准备检测\")\n",
    "    # 转为灰度图像\n",
    "    if ret:\n",
    "        args[\"image\"] = frame\n",
    "        im = predict(args)\n",
    "        cv2.waitKey(5)\n",
    "        \n",
    "#     while True:\n",
    "#         args = {}\n",
    "#         # 读取当前帧\n",
    "#         ret, frame = camera.read()\n",
    "#         # 转为灰度图像\n",
    "#         if ret:\n",
    "#             args[\"image\"] = frame\n",
    "#             #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
    "#             im = predict(args)\n",
    "#             cv2.waitKey(5)\n",
    "#             break\n",
    "        \n",
    "        \n",
    "            \n",
    "        \n",
    "#     camera.release()\n",
    "#     cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#识别人脸 \n",
    "cam = cv2.VideoCapture(0)\n",
    "cascade_path = 'haarcascade_profileface.xml'\n",
    "face_cascade = cv2.CascadeClassifier(cascade_path)\n",
    "    \n",
    "    \n",
    "    \n",
    "def recognition(img):\n",
    "    faces = face_cascade.detectMultiScale(img, 1.1, 6)\n",
    "    #print(faces)\n",
    "\n",
    "    if len(faces) > 0:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "        \n",
    "        \n",
    "\n",
    "#二维码识别\n",
    "def scan_qrcode(img):\n",
    "    barcodes = pyzbar.decode(img)\n",
    "    barcodeData = None\n",
    "    found = False\n",
    "    for barcode in barcodes:\n",
    "        barcodeData = barcode.data.decode(\"utf-8\")\n",
    "        print(barcodeData)\n",
    "        if barcodeData in white_list:\n",
    "            found = True\n",
    "            break\n",
    "    return found, barcodeData\n",
    "\n",
    "def goable():\n",
    "    has_face=False\n",
    "    \n",
    "    while flag:\n",
    "        ret, frame = cam.read()\n",
    "        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
    "        has_face = recognition(gray)\n",
    "        print(has_face)\n",
    "        if has_face:\n",
    "            whistle()\n",
    "            wink()\n",
    "            flag=False\n",
    "            cam.release()\n",
    "            return True\n",
    "            break\n",
    "\n",
    "        \n",
    "        return False\n",
    "def go():\n",
    "    \n",
    "    #打开级联分类器模型文件\n",
    "    \n",
    "#     find_qrcode=False\n",
    "#     flag=True\n",
    "    \n",
    "    \n",
    "    has_face=goable()\n",
    "    \n",
    "#     ret, frame2 = cam.read()\n",
    "#     gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)\n",
    "#     find_qrcode, qrcodeData = scan_qrcode(gray)\n",
    "    if has_face:\n",
    "        try:\n",
    "            init()\n",
    "            image_widget = widgets.Image(format='jpeg', width=320, height=240)\n",
    "            display(image_widget)\n",
    "            print(\"开始运行主程序\")\n",
    "            while True:\n",
    "\n",
    "                print(1)\n",
    "        #             ret, frame2 = cam.read()\n",
    "        #             gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)\n",
    "        #             find_qrcode, qrcodeData = scan_qrcode(gray)\n",
    "                distance = Distance_test()\n",
    "        #             if find_qrcode:\n",
    "        #                  whistle_15s()\n",
    "                if distance < 60:\n",
    "                    detect()\n",
    "                    print(\"小于30的避障\")\n",
    "                    servo_color_carstate()\n",
    "                else:\n",
    "                    print(\"bigger than 30\")\n",
    "                    detect()\n",
    "                    run(5, 5)\n",
    "                \n",
    "\n",
    "        except KeyboardInterrupt:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ab4db0c27a434f4fbff61a3e1c11a660",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Image(value=b'', format='jpeg', height='240', width='320')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始运行主程序\n",
      "1\n",
      "distance is 560 \n",
      "bigger than 30\n",
      "检测开始\n",
      "middle  _______________________________\n",
      "准备检测\n",
      "[2]\n",
      "[2]: 75.20%\n",
      "1\n",
      "distance is 560 \n",
      "bigger than 30\n",
      "检测开始\n",
      "middle  _______________________________\n",
      "准备检测\n",
      "[2]\n",
      "[2]: 88.91%\n",
      "1\n",
      "distance is 106 \n",
      "bigger than 30\n",
      "检测开始\n",
      "middle  _______________________________\n",
      "准备检测\n",
      "[2]\n",
      "[2]: 43.29%\n",
      "1\n",
      "distance is 588 \n",
      "bigger than 30\n",
      "检测开始\n",
      "middle  _______________________________\n"
     ]
    }
   ],
   "source": [
    "go()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
